package com.wtone.platform.gat.images.gat.service.impl;

import cn.hutool.core.util.IdUtil;
import com.wtone.platform.gat.images.gat.config.RedisUtils;
import com.wtone.platform.gat.images.gat.core.Constants;
import com.wtone.platform.gat.images.gat.core.Response;
import com.wtone.platform.gat.images.gat.model.KeepaliveObject;
import com.wtone.platform.gat.images.gat.model.RegisterObject;
import com.wtone.platform.gat.images.gat.model.ResponseStatusObject;
import com.wtone.platform.gat.images.gat.model.UnRegisterObject;
import com.wtone.platform.gat.images.gat.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: aiLin
 * @Date: 2020/9/27 11:33
 * @desc: 注册 | 注销 | 心跳
 */
@Slf4j
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public ResponseStatusObject register(RegisterObject registerObject) {
        String token = IdUtil.fastSimpleUUID();
        String reqUrl = "/VIID/System/Register";
        redisUtils.set(Constants.TOKENPREFIX+registerObject.getRegister().getDeviceID(),registerObject.getRegister().getDeviceID(), Constants.expire);
        log.info("/VIID/System/Register:"+Constants.TOKENPREFIX+registerObject.getRegister().getDeviceID());
        return Response.ok(registerObject.getRegister().getDeviceID(),reqUrl,"注册成功 User-Identify："+registerObject.getRegister().getDeviceID());
    }


    /***
     * 当请求方无需和视图库进行通信时，可以请求注销接口，撤销注册消息。
     * @param unRegisterObject
     * @return
     */
    @Override
    public ResponseStatusObject unRegister(UnRegisterObject unRegisterObject) {
        String reqUrl = "/VIID/System/UnRegister";
        redisUtils.delete(Constants.TOKENPREFIX+unRegisterObject.getUnRegister().getDeviceID());
        return Response.ok(unRegisterObject.getUnRegister().getDeviceID(),reqUrl,"注销成功");
    }

    /***
     * 心跳保活接口
     * 注册成功后，会有一定的有效期（一般为5分钟），在有效期内，如果没有接收到新的请求，注册信息将会失效，
     * 下次访问将需要重新注册；当然任何形式的成功请求，都能触发保活机制，重置有效期。
     * @param keepaliveObject
     * @return
     */
    @Override
    public ResponseStatusObject keepalive(KeepaliveObject keepaliveObject) {
        String reqUrl = "/VIID/System/Keepalive";
        return Response.ok(keepaliveObject.getKeepalive().getDeviceID(),reqUrl,"保活成功");
    }

}
